import { DependencyContainer, toSuccess, useTransaction } from "@sker/core"
import { AutoDevice, AutoFile, SysMenu, AutoApp } from "@sker/entities"
import { Like } from "typeorm"
import { useDataSource } from '@sker/orm'
export default async (injector: DependencyContainer) => {
    const ds = await useDataSource([
        AutoFile,
        AutoDevice,
        AutoApp
    ], injector, true)
    await ds.destroy();
    await useTransaction(async m => {
        // 检查是否有s3菜单
        const menus = await m.find(SysMenu, { where: { name: Like('AutojsCore%') } })
        if (menus.length > 0) {
            await m.delete(SysMenu, menus.map(it => it.id))
        }
        const addonMenus: any[] = [
            { name: 'AutojsCore', title: '远控手机', url: null, main: null, is_group: true },
            { name: 'AutojsCoreWebControl', title: '中控台', pname: 'AutojsCore', url: '/autojs/core/web/control', main: '/@autojs/core/public/web/control.js', is_group: false },
            { name: 'AutojsCoreWebKeyword', title: '关键字管理', pname: 'AutojsCore', url: '/autojs/core/web/keyword', main: '/@autojs/core/public/web/keyword.js', is_group: false },
            { name: 'AutojsCoreWebDevice', title: '设备管理', pname: 'AutojsCore', url: '/autojs/core/web/device', main: '/@autojs/core/public/web/device.js', is_group: false },
            { name: 'AutojsCoreWebScript', title: '脚本管理', pname: 'AutojsCore', url: '/autojs/core/web/script', main: '/@autojs/core/public/web/script.js', is_group: false },
            { name: 'AutojsCoreWebTask', title: '任务管理', pname: 'AutojsCore', url: '/autojs/core/web/task', main: '/@autojs/core/public/web/task.js', is_group: false },
            { name: 'AutojsCoreWebUser', title: '用户管理', pname: 'AutojsCore', url: '/autojs/core/web/user', main: '/@autojs/core/public/web/user.js', is_group: false },
        ]
        const toSave = addonMenus.map(menu => {
            const item = menus.find(it => it.name === menu.name)
            if (item) {
                menu.id = item.id;
            }
            return menu;
        })
        await m.save(SysMenu, toSave)
    })
    return toSuccess(true)
}